import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

import javax.swing.SwingUtilities;

/**
 * @author shilpa
 * 
 */
public class TKeyListener implements KeyListener {

	TwinkleApp twinkleApp;
	TPanel tPanel;
	LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
	Map<String, Element> charElementMap = new ConcurrentHashMap<String, Element>();

	public TKeyListener(TwinkleApp main) {
		twinkleApp = main;
		tPanel = main.getTPanel();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// System.out.println(" key pressed "+ e.getKeyCode());
	}

	@Override
	public void keyReleased(KeyEvent e) {
		System.out.println(" key released " + e.getKeyCode() + " keytext= "
				+ e.getKeyChar());
	//	if (!twinkleApp.hasKeyElements()) {
			int keyCode = e.getKeyCode();
			System.out.println("adding to the list = " + keyCode);
			// keyList.add(keyCode);
			queue.add(keyCode);
		//	return;
	//	}

		// showElement(e);
	}

	@Override
	public void keyTyped(KeyEvent e) {

		if (!twinkleApp.hasKeyElements()) {
			// keyReleased in working so you just stay away!!
			return;
		}
		System.out.println(" key typed " + e.getKeyCode() + " keytext= "
				+ e.getKeyChar());
		
		showElement(e);

	}

	public synchronized void showElement(KeyEvent e) {
		
		String key = String.valueOf(e.getKeyChar());
		System.out.println("looking for " + key);
		Element element = getElement(key);
		if (element == null) {
			System.out.println("element is null");
			String text = "Invalid Input";
			// tPanel.drawCenteredString(text, "red");
		} else {
			String image = element.getImageFile();
			String sound = element.getSoundFile();
			System.out.println("Repainting..");
			tPanel.displayImageAndText(image, element.getElementName(), element
					.getColor());
			tPanel.playSound(sound);
		}
	}
	
	public boolean hasKeyElements() {
		return charElementMap.size() != 0;
	}

	public synchronized Integer getKeyTyped() {
//		if (twinkleApp.hasKeyElements()) {
//			throw new RuntimeException(
//					"The main listener is disabled as activity listener is enabled.");
//		}
		try {
			return queue.take();
		} catch (InterruptedException e) {
			System.out.println("" + e);
		}
		return -1;
	}
	
	public synchronized void reset() {
		queue.clear();
		charElementMap.clear();
	}

	public synchronized void putKeyTyped(Integer k) {
//		if (twinkleApp.hasKeyElements()) {
//			throw new RuntimeException(
//					"The main listener is disabled as activity listener is enabled.");
//		}
		try {
			queue.put(k);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void addElement(String key, String word, String image, String sound,
			String color) {
		System.out.println("adding element " + key + " " + word + " " + image
				+ " " + sound + " " + color);
		charElementMap.put(key, new Element(word, image, sound, color));
	}

	public void addElement(String key, String word, String image, String sound) {
		System.out.println("adding element " + key + " " + word + " " + image
				+ " " + sound + " ");
		charElementMap.put(key, new Element(word, image, sound, null));
	}

	public Element getElement(String key) {
		return charElementMap.get(key);
	}
}
